<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <!-- <el-form-item label="类型" prop="type">
            <el-select v-model="queryParams.type" placeholder="请选择类型，0固定，1非固定" clearable>
              <el-option v-for="dict in asset_type" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item> -->
          <el-form-item label="类别" prop="category">
            <el-select v-model="queryParams.category" placeholder="请选择类别" clearable>
              <el-option v-for="item in categoryData" :key="item.id" :label="item.category" :value="item.id" />
            </el-select>
            <!-- <el-select class="slese" @visible-change="cxhandleVisibleChange" v-model="queryParams.category" placeholder="请选择类别" ref="selectRef">
              <el-option class="cxLedgerDropdown" v-for="item in categoryData" :key="item.id" :label="item.category" :value="item.id" />
              <p class="el-select-dropdown__empty" v-if="loadingmore">加载中</p>
              <p class="el-select-dropdown__empty" v-if="loadingnomore">无数据</p>
            </el-select> -->
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <!-- <el-form-item label="型号" prop="model">
            <el-input v-model="queryParams.model" placeholder="请输入型号" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="单位" prop="unit">
            <el-input v-model="queryParams.unit" placeholder="请输入单位" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="数量" prop="quantity">
            <el-input v-model="queryParams.quantity" placeholder="请输入数量" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="单价" prop="univalence">
            <el-input v-model="queryParams.univalence" placeholder="请输入单价" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="合计" prop="total">
            <el-input v-model="queryParams.total" placeholder="请输入合计" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item> -->
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['business:assetLedger:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="assetLedgerList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="类型" align="center" prop="type">
          <template #default="scope">
            <dict-tag :options="asset_type" :value="scope.row.type" />
          </template>
        </el-table-column>
        <!-- <el-table-column label="目录编号" align="center" prop="id" v-if="true" /> -->
        <!-- <el-table-column label="类型" align="center" prop="type">
          <template #default="scope">
            <dict-tag :options="asset_type" :value="scope.row.type" />
          </template>
        </el-table-column> -->
        <el-table-column label="类别" align="center" prop="category">
          <template #default="scope">
            <span>{{getCategory(scope.row.category)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="名称" align="center" prop="name" />
        <el-table-column label="型号" align="center" prop="model" />
        <el-table-column label="单位" align="center" prop="unit" />
        <el-table-column label="数量" align="center" prop="quantity" />
        <el-table-column label="单价" align="center" prop="univalence" />
        <el-table-column label="合计" align="center" prop="total" />
        <el-table-column label="备注" align="center" prop="remark" />
      </el-table>

      <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { listAssetLedger } from '@/api/business/assetLedger';
import { AssetLedgerVO, AssetLedgerQuery, AssetLedgerForm } from '@/api/business/assetLedger/types';
import { listAssetCategory } from '@/api/business/assetCategory/index';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { asset_type } = toRefs<any>(proxy?.useDict('asset_type'));

const assetLedgerList = ref<AssetLedgerVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const categoryData = ref([])
/* const loadingmore = ref(false)
const loadingnomore = ref(false)
const xzqueryParams = ref<ProjectQuery>({
  pageNum: 1,
  pageSize: 10,
  type:2
})
const xztotal = ref(0); */

const queryFormRef = ref<ElFormInstance>();

const initFormData: AssetLedgerForm = {
  id: undefined,
  type: "2",
  category: undefined,
  name: undefined,
  model: undefined,
  unit: undefined,
  quantity: undefined,
  univalence: undefined,
  total: undefined,
  remark: undefined,
}
const data = reactive<PageData<AssetLedgerForm, AssetLedgerQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    type: "2",
    category: undefined,
    name: undefined,
    model: undefined,
    unit: undefined,
    quantity: undefined,
    univalence: undefined,
    total: undefined,
    params: {
    }
  },
});

const { queryParams, form } = toRefs(data);

/** 查询资产台账列表 */
const getList = async () => {
  loading.value = true;
  const res = await listAssetLedger(queryParams.value);
  assetLedgerList.value = res.rows.filter( item => {
    if(item.quantity != 0)return item
  })
  total.value = res.total;
  loading.value = false;
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: AssetLedgerVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('business/assetLedger/export', {
    ...queryParams.value
  }, `非固定资产台账_${new Date().getTime()}.xlsx`)
}

const getCategory = (data) =>{
  if (data && categoryData.value) {
    let obj = categoryData.value.find(item=>item.id ==data)
    return obj?.category
  }
}

const getfixedCategory = async () =>{
  const res = await listAssetCategory();
  categoryData.value = res.rows
  // xztotal.value = res.total;
}

/* // 下拉懒加载
const cxhandleVisibleChange = (visible: any) => {
  try {
    if (visible) {
      // 如果下拉框显示
      let xzDropdown: any = document.getElementsByClassName('cxLedgerDropdown')[0].parentElement.parentElement
      // 监听下拉框滚动事件
      xzDropdown.addEventListener('scroll', cxloadNextPage)
    } else {
      // 如果下拉框隐藏
      let xzDropdown: any = document.getElementsByClassName('cxLedgerDropdown')[0].parentElement.parentElement
      // 移除下拉框滚动事件监听
      xzDropdown.removeEventListener('scroll', cxloadNextPage)
    }
  } catch (error) {}
}

const cxloadNextPage = async () => {
  let xzDropdown: any = document.getElementsByClassName('cxLedgerDropdown')[0].parentElement.parentElement;
  // 判断下拉框是否触底
  if (xzDropdown.scrollTop + xzDropdown.clientHeight >= xzDropdown.scrollHeight - 2) {
    loadingnomore.value = false
    loadingmore.value = true
    if (categoryData.value.length + 1 <= xztotal.value && loadingmore.value) {
      xzqueryParams.value.pageNum ++
      const res = await listAssetCategory(xzqueryParams.value)
      categoryData.value.length + 1   <= xztotal.value?categoryData.value.push(...res.rows):''
      loadingmore.value = false
    }else{
      loadingmore.value = false
      xzDropdown.removeEventListener('scroll', cxloadNextPage)
    loadingnomore.value = true
    }
  } else {
    loadingnomore.value = true
  }
} */

onMounted(async() => {
  await getfixedCategory()
  await getList();
});
</script>
